package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.utils.ExcelUtils;
import com.company.project.service.impl.EmployeeServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.io.IOException;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.EmployeeEntity;
import com.company.project.service.EmployeeService;
import org.springframework.web.multipart.MultipartFile;


/**
 * ${comments}
 *
 * @author zlf
 * @email
 * @date 2020-12-07 13:39:06
 */
@Controller
@RequestMapping("/")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmployeeServiceImpl EmployeeServiceImpl;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/employee")
    public String employee() {
        return "employee/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("employee/add")
    @RequiresPermissions("employee:add")
    @ResponseBody
    public DataResult add(@RequestBody EmployeeEntity employee){
      //  EmployeeServiceImpl.insertEemployee(employee);

        employeeService.save(employee);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("employee/delete")
    @RequiresPermissions("employee:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        employeeService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("employee/update")
    @RequiresPermissions("employee:update")
    @ResponseBody
    public DataResult update(@RequestBody EmployeeEntity employee){
        employeeService.updateById(employee);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("employee/listByPage")
    @RequiresPermissions("employee:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody EmployeeEntity employee){
        Page page = new Page(employee.getPage(), employee.getLimit());
        LambdaQueryWrapper<EmployeeEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件
        if (!StringUtils.isEmpty(employee.getName())) {
            queryWrapper.like(EmployeeEntity::getName, employee.getName());
        }
        //queryWrapper.eq(EmployeeEntity::getId, employee.getId());
        IPage<EmployeeEntity> iPage = employeeService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "上传员工明细Excel")
    @PostMapping("employee/upload")
    @RequiresPermissions(value = {"employee:import"})
    @ResponseBody
    public DataResult upload(@RequestParam(value = "file") MultipartFile file) {
        //判断文件是否空
        if (file == null || file.getOriginalFilename() == null || "".equalsIgnoreCase(file.getOriginalFilename().trim())) {
            return DataResult.fail("文件为空");
        }
        System.out.println(file.getOriginalFilename());
        try {
            List<EmployeeEntity> result = ExcelUtils.importExcel(file, EmployeeEntity.class);
            System.out.println(result);
            for (EmployeeEntity employee : result) {
                // 将导入的数据做保存数据库操作
                employeeService.save(employee);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return DataResult.success();
    }

}
